package com.hieu.sms.web.mb;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;

import com.hieu.sms.model.Course;
import com.hieu.sms.model.CourseScoreFactor;
import com.hieu.sms.model.Department;
import com.hieu.sms.model.filter.CourseFilter;
import com.hieu.sms.services.InformationServices;
import com.hieu.sms.utils.TextUtil;
import com.hieu.sms.web.utils.FacesContextUtils;

@ManagedBean(name = "mbCourse")
@ViewScoped
public class MbCourse extends AbstractManagedBeanSimple<Course, CourseFilter> {

	private Map<String, String> depMap;

	private Map<String, String> courseMap;

	private List<CourseScoreFactor> courseScoreFactors;

	@ManagedProperty("#{informationServices}")
	private InformationServices informationServices;

	public void setInformationServices(InformationServices informationServices) {
		this.informationServices = informationServices;
	}

	@PostConstruct
	public void init() {
		try {
			List<Department> departments = informationServices.list();
			depMap = new HashMap<String, String>(departments.size());
			for (Department department : departments) {
				depMap.put(department.getName(), department.getDepId());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected List<Course> searchListCallback(CourseFilter filter)
			throws Exception {
		this.courseScoreFactors = Collections.emptyList();
		return informationServices.findCourse(filter);
	}

	@Override
	protected String deleteCallback(Course item) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String addCallback(Course item) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String modifyCallback(Course item) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String deleteCallback(String id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getId(Course item) {
		// TODO Auto-generated method stub
		return null;
	}

	public void onRowSelect() throws Exception {
		this.courseScoreFactors = informationServices
				.findCourseScoreFactor(selected);
		this.courseScoreFactorSelected = null;
	}

	public void handleDepartmentSelected() throws Exception {
		if (TextUtil.isNotEmpty(filter.getDepId())) {
			List<Course> courses = informationServices.findCourse(filter
					.getDepId());
			courseMap = new HashMap<String, String>();
			for (Course course : courses) {
				courseMap.put(course.getName(), course.getCourseId());
			}
		}
	}

	public Map<String, String> getDepMap() {
		return depMap;
	}

	public void setDepMap(Map<String, String> depMap) {
		this.depMap = depMap;
	}

	public Map<String, String> getCourseMap() {
		return courseMap;
	}

	public void setCourseMap(Map<String, String> courseMap) {
		this.courseMap = courseMap;
	}

	public List<CourseScoreFactor> getCourseScoreFactors() {
		return courseScoreFactors;
	}

	public CourseScoreFactor getCourseScoreFactorSelected() {
		return courseScoreFactorSelected;
	}

	public void setCourseScoreFactorSelected(
			CourseScoreFactor courseScoreFactorSelected) {
		this.courseScoreFactorSelected = courseScoreFactorSelected;
	}

	public CourseScoreFactor getCourseScoreFactor() {
		return courseScoreFactor;
	}

	public void setCourseScoreFactor(CourseScoreFactor courseScoreFactor) {
		this.courseScoreFactor = courseScoreFactor;
	}

	private CourseScoreFactor courseScoreFactorSelected;

	private CourseScoreFactor courseScoreFactor = new CourseScoreFactor();

	public void prepareAddCourseScoreFactor() {
		courseScoreFactor = new CourseScoreFactor();
		if (selected != null) {
			courseScoreFactor.setDepId(selected.getDepId());
			courseScoreFactor.setCourseId(selected.getCourseId());
		}
	}
	
	public void prepareEditCourseScoreFactor(){
		courseScoreFactor = courseScoreFactorSelected;
	}

	public void onFactorRowSelect(SelectEvent selectEvent) {
		courseScoreFactor = courseScoreFactorSelected;
	}

	public void handleCourseScoreFactorWindowClose() {

	}

	public void doDeleteCourseScoreFactor() throws Exception {
		informationServices.delete(courseScoreFactor);
		this.courseScoreFactors = informationServices
				.findCourseScoreFactor(selected);
		courseScoreFactorSelected = null;
	}

	public void doAddCourseScoreFactor() throws Exception {
		boolean validate = validateCourseScoreFactor(courseScoreFactor);
		if (validate) {
			informationServices.add(courseScoreFactor);
			this.courseScoreFactors = informationServices
					.findCourseScoreFactor(selected);
			courseScoreFactorSelected = null;
		} else {

			FacesContextUtils
					.addErrorExceptionMessage("Tong He So Diem Phai Bang 100.");

		}
		RequestContext context = RequestContext.getCurrentInstance();
		context.addCallbackParam("validate", validate);
	}

	private boolean validateCourseScoreFactor(
			CourseScoreFactor courseScoreFactor) {
		float total = 0;
		if (courseScoreFactor.getMidTestFactor() != null) {
			total += courseScoreFactor.getMidTestFactor();
		}
		if (courseScoreFactor.getEssaysFactor() != null) {
			total += courseScoreFactor.getEssaysFactor();
		}
		if (courseScoreFactor.getFinalTestFactor() != null) {
			total += courseScoreFactor.getFinalTestFactor();
		}
		return total == 100 ? true : false;
	}

	public boolean isHasFactor() {
		if (selected == null)
			return true;
		return (this.courseScoreFactors != null && this.courseScoreFactors
				.size() > 0) ? true : false;
	}

}
